package hidalgo.expenses.adapter;

import hidalgo.expenses.entities.Note;
import hidalgo.expenses.widgets.NoteItemView;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class NoteViewAdapter extends ArrayAdapter<Note> {

	private Context context;
	private ArrayList<Note> noteList;

	public NoteViewAdapter(Context context, int textViewResourceId,
			ArrayList<Note> noteList) {
		super(context, textViewResourceId);
		this.context = context;
		this.noteList = noteList;

	}

	@Override
	public int getCount() {
		return noteList.size();
	}

	@Override
	public Note getItem(int position) {
		return noteList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return noteList.get(position).getNoteId();
	}

	public void updateNote(Note note) {
		for (int i = 0; i < noteList.size(); i++) {
			if (noteList.get(i).getNoteId() == note.getNoteId()) {
				noteList.set(i, note);
				break;
			}
		}
	}

	public void addNote(Note note) {
		noteList.add(note);
	}

	public void removeNote(int noteId) {

		for (int i = 0; i < noteList.size(); i++) {
			if (noteList.get(i).getNoteId() == noteId) {
				noteList.remove(i);
				break;
			}
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		NoteItemView noteItemView = new NoteItemView(context);
		noteItemView.setNote(noteList.get(position));

		return noteItemView;
	}

}
